/*
 * 版权所有 (C) 2001-2014 深圳市艾派应用系统有限公司。保留所有权利。
 * 版本：
 * 修改记录：
 *      1、2014-11-21，tfl创建。 
 */
package com.chinamobile.ln.ictenterprises.db;
import java.util.HashMap;
import java.util.Map;

import com.chinamobile.ln.ictenterprises.cache.Cache;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DBUtils
{
    private DatabaseHelper mDatabaseHelper;
    private Context context;
    private SQLiteDatabase db;
    private static final String CACHE_DB_NAME = "ict_enterprises";
    private int DB_VERSION = 1;

    public DBUtils(Context app)
    {
        this.context = app;
        Map<String, String> sqls = new HashMap<String, String>();
        sqls.put(Cache.TABLE_NAME, Cache.SQLNAME);
        mDatabaseHelper = new DatabaseHelper(context, CACHE_DB_NAME, null,
            DB_VERSION, sqls);
    }

    /**
     * 打开数据库
     */
    public DBUtils open() throws SQLException
    {
        db = mDatabaseHelper.getWritableDatabase();
        return this;
    }

    /**
     * 关闭数据库
     */
    public void close()
    {
        mDatabaseHelper.close();
    }

    /**
     * 插入数据
     */
    public long insert(String tableName, ContentValues initialValues)
    {
        long i = db.insert(tableName, null, initialValues);
        return i;
    }

    /**
     * 删除数据
     */
    public boolean delete(String tableName, String whereClause)
    {
        boolean flag = db.delete(tableName, whereClause, null) > 0;
        return flag;
    }

    /**
     * 更新数据
     */
    public boolean update(String tableName, String whereClause,
        ContentValues args)
    {
        boolean flag = db.update(tableName, args, whereClause, null) > 0;
        return flag;
    }

    /**
     * 查询数据
     */
    public Cursor query(String tableName, String[] columns, String querySql)
    {
        Cursor mCursor = db.query(true, tableName, columns, querySql, null,
            null, null, null, null);

        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public Cursor queryAll(String tableName, String[] columns)
    {
        Cursor mCursor = db.query(true, tableName, columns, null, null, null,
            null, null, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public Cursor queryAllByOrderBy(String tableName, String[] columns,
        String orderBy)
    {
        Cursor mCursor = db.query(true, tableName, columns, null, null, null,
            null, orderBy, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    public Cursor queryAllByOrderByAndWhere(String tableName, String[] columns,
        String orderBy, String selection)
    {
        Cursor mCursor = db.query(true, tableName, columns, selection, null,
            null, null, orderBy, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    private class DatabaseHelper extends SQLiteOpenHelper
    {
        private Map<String, String> sqls = null;

        public DatabaseHelper(Context context, String name,
            CursorFactory factory, int version, Map<String, String> sqls)
        {
            super(context, name, factory, version);
            this.sqls = sqls;
        }

        @Override
        public void onCreate(SQLiteDatabase db)
        {
            if (sqls != null && sqls.size() > 0)
            {
                for (String table : sqls.values())
                {
                    db.execSQL(table);
                }
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            if (newVersion > oldVersion)
            {
                if (sqls != null && sqls.size() > 0)
                {
                    for (String table : sqls.keySet())
                    {
                        db.execSQL("DROP TABLE IF EXISTS  " + table);
                    }
                }
                onCreate(db);
            }
        }
    }
}
